package com.yc.Testjvm.classload;

import com.yc.Testjvm.classload.customer.MyClassLoader;
import com.yc.Testjvm.classload.demo.JavaClassExecuter;
import com.yc.entity.Person;
import sun.applet.AppletClassLoader;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;


public class Test3 {

    public static void main(String[] args) {
        Test3 t = new Test3();
        t.m1();
//        t.m3();
    }
    
    /**
     * 使用类加载器，手动加载一个外部class，并执行
     */
    public void m1() {
        try {
            //读取外部class文件流
            InputStream is = new FileInputStream("/Users/yc/ws/source/HelloWord.class");
            byte[] b = new byte[is.available()];
            is.read(b);
            is.close();

            //使用自定义类加载器加载这个class
//            AppletClassLoader cl = (AppletClassLoader)this.getClass().getClassLoader();
            MyClassLoader mc = new MyClassLoader();

            Class c = mc.load(b);
            System.out.println("----classloader=" + c.getClassLoader());
            System.out.println("----classloader.parent=" + c.getClassLoader().getParent());
            System.out.println("----ContextClassLoader=" + Thread.currentThread().getContextClassLoader());

            Object o = c.newInstance();


            //使用反射调用这个类里的静态方法
//            Method m = c.getMethod("main", new Class[]{String[].class});
//            m.invoke(null, new String[] {null});

            //实例化出来？
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
//        } catch (NoSuchMethodException e) {
//            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
//        } catch (InvocationTargetException e) {
//            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }

    public void m2() {
        try {
            Class.forName("");
            Connection conn = DriverManager.getConnection("", "", "");
            PreparedStatement st = conn.prepareStatement("");
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }

    public void m3() {
        System.out.println("-----1");
        try {
            Thread.sleep(30000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("加载Person");
        Person p = new Person("zs", 12);
        try {
            Thread.sleep(240000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("-----2");
    }
}


